package com.tim.service.Impl;

import com.tim.dao.HouseDao;
import com.tim.dao.Impl.HouseDaoImpl;
import com.tim.pojo.DataResult;
import com.tim.pojo.House;
import com.tim.service.HouseService;

import java.sql.SQLException;
import java.util.List;

/**
 * 作者：Tim
 * 时间：2021/4/26 19:42
 */
public class HouseServiceImpl implements HouseService {
    HouseDao houseDao = new HouseDaoImpl();
    @Override
    public int deleteHouse(String houseID) {
        try {
            int id = Integer.parseInt(houseID);
            return houseDao.deleteHouse(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addHouse(House house) {
        try {
            return houseDao.addHouse(house);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public House selectOne(String houseID) {
        try {
            int id = Integer.parseInt(houseID);
            return houseDao.selectOne(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateHouse(House house) {
        try {
            return houseDao.updateHouse(house);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public DataResult limit(String currentPage, String pageSize, String houseName) {
        try {
            int p =1 ;      //当前页
            int s = 5;      //每页数目
            if(currentPage!=null){
                p = Integer.parseInt(currentPage);
            }
            if(pageSize!=null){
                s= Integer.parseInt(pageSize);
            }
            List<House> houseList = houseDao.limit(p,s,houseName);
            DataResult dataResult = new DataResult(0,"查询成功",houseList, houseDao.count(houseName));
            return dataResult;
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        DataResult dataResult = new DataResult(40000,"不好意思，数据接口错误");
        return dataResult;
    }
}
